<template>
  <div class="app-container">
    <el-row>
      <el-col :span="12">
        <div class="grid-content bg-purple">
          <el-button type="success" @click="onNewly">新增</el-button>
          <el-button type="warning" @click="onDeleAll">多项删除</el-button>
        </div>
      </el-col>
      <el-col :span="12">
        <div class="grid-content bg-purple-light">
          <el-input
            placeholder="请输入内容"
            v-model="searchVal"
            class="input-with-select"
          >
            <el-button
              slot="append"
              @click="onSearch"
              icon="el-icon-search"
            ></el-button>
          </el-input>
        </div>
      </el-col>
    </el-row>
    <el-table
      :data="list"
      border
      style="width: 100%; margin: 10px auto"
      @selection-change="handleSelectionChange"
      @sort-change="onSortChange"
      :row-class-name="tableRowClassName"
      :row-style="selectedHighlight"
      @row-click="rowClick"
      height="500"
    >
      <el-table-column fixed type="selection" width="50"> </el-table-column>
      <el-table-column fixed type="index" label="#" width="50">
      </el-table-column>
      <el-table-column fixed prop="title" label="标题" width="300">
      </el-table-column>
      <el-table-column prop="type" label="类型" width="200"> </el-table-column>
      <el-table-column prop="author" label="作者" width="200">
      </el-table-column>
      <el-table-column prop="time" label="时间" sortable width="150">
      </el-table-column>
      <el-table-column prop="date" label="日期" sortable width="200">
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="300">
        <template slot-scope="scope">
          <el-button @click="onChaKan(scope.row)" type="text" size="small"
            >查看</el-button
          >
          <el-button @click="onDelete(scope.row.id)" type="text" size="small"
            >删除</el-button
          >
          <el-button
            @click="handleClick(scope)"
            type="text"
            ref="updateBtn"
            size="small"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 抽屉 -->
    <el-drawer
      :title="title"
      :visible.sync="drawer"
      :before-close="handleClose"
      :with-header="true"
      :size="'50%'"
    >
      <el-form
        :model="form"
        size="mini"
        style="padding: 0 5%"
        ref="form"
        label-width="100px"
        class="demo-form"
      >
        <el-form-item label="标题" prop="name">
          <el-input v-model="form.title" :disabled="look"></el-input>
        </el-form-item>
        <el-form-item label="类别" prop="resource">
          <el-select
            v-model="form.cid"
            :disabled="look"
            placeholder="请选择类别"
          >
            <el-option
              v-for="(v, i) in tableData"
              :key="i"
              :label="v.name"
              :value="v.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="作者" prop="name">
          <el-input :disabled="true" v-model="form.author"></el-input>
        </el-form-item>
        <el-form-item label="用时" prop="desc">
          <el-select
            v-model="form.time"
            :disabled="look"
            placeholder="请选择用时"
          >
            <el-option label="一分钟" value="1"></el-option>
            <el-option label="二分钟" value="2"></el-option>
            <el-option label="三分钟" value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="内容">
          <mavonEditor
            style="max-height: 300px; overflow-y: scroll"
            v-model="form.content"
          ></mavonEditor>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :disabled="look" @click="submitForm(form)"
            >立即创建</el-button
          >
          <el-button @click="resetForm()">关闭</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[3, 5, 10, 15]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import { mapState, mapActions, mapMutations } from "vuex";
import { mavonEditor } from "mavon-editor";
import "mavon-editor/dist/css/index.css";
export default {
  layout: "main",
  data() {
    return {
      drawer: false,
      form: {
        author: "situ",
      },
      title: "新增",
      searchVal: "",
      look: false,
      ids: [],
    };
  },
  async mounted() {
    await this.getMsbdList();
    this.getClassify();
  },
  methods: {
    ...mapActions("msbd", [
      "getMsbdList",
      "addMsbdList",
      "delMsbdList",
      "updataMsbdList",
    ]),
    ...mapActions("classify", ["getClassify"]),
    ...mapMutations("msbd", ["PAGE_SIZE", "CURRENT_PAGE", "SET_TOTAL"]),
    tableRowClassName({ row, rowIndex }) {},
    rowClick(row) {},
    selectedHighlight({ row, rowIndex }) {},
    //新增开关
    onNewly() {
      this.drawer = true;
    },
    //修改
    handleClick(row) {
      // console.log({...row});
      this.form = JSON.parse(JSON.stringify(row.row));
      this.drawer = true;
    },
    //删除
    onDelete(value) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          await this.delMsbdList(value);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //删除
    onDeleAll() {
      this.delMsbdList(this.id);
    },
    handleSelectionChange(id) {
      let arr = [];
      for (var i = 0; i < id.length; i++) {
        arr.push(id[i].id);
      }
      this.id = arr.join(",");
      console.log(this.id);
    },
    //查看
    onChaKan(value) {
      //   console.log(value);
      this.drawer = true;
      this.form = value;
    },
    //搜索
    onSearch() {
      //   console.log(this.searchVal);
      //   this.SEACH_DATA(this.searchVal);
      //   this.getList();
    },
    //排序
    onSortChange(value) {},
    //抽屉确定
    submitForm() {
      // this.addMsbdList(this.form)
      if (this.form.id) {
        this.updataMsbdList(this.form);
      } else {
        this.addMsbdList(this.form);
      }
      this.getMsbdList();
      this.form = {
        author: "situ",
      };
      this.drawer = false;
    },
    //抽屉取消
    resetForm() {
      this.drawer = false;
      this.form = { author: "situ" };
    },
    //抽屉关闭
    handleClose(done) {
      this.drawer = false;
    },
    handleSizeChange(val) {
      console.log(val);
      this.PAGE_SIZE(val);
      this.getMsbdList();
    },
    handleCurrentChange(val) {
      console.log(val);
      this.CURRENT_PAGE(val);
      this.getMsbdList();
    },
  },
  computed: {
    ...mapState("msbd", ["list", "pageSize", "currentPage", "total"]),
    ...mapState("classify", ["tableData"]),
  },
  components: {
    mavonEditor,
  },
};
</script>

<style>
.app-container {
  display: grid;
  padding: 10px 30px;
}

.el-drawer__header span:focus {
  outline: none;
}

.el-dialog__close {
  outline: none !important;
}
</style>
